package com.rongshu.view.discovery.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.utils.GlideUtils;
import com.rongshu.view.base.AdapterListener;
import com.rongshu.view.discovery.activity.SerialDetailsActivity;
import com.rongshu.view.discovery.bean.SerialListResponse;

import java.util.List;

/**
 * created by zhengweis on 2018/11/1.
 * description：
 */
public class SerialListAdapter extends RecyclerView.Adapter<SerialListAdapter.ViewHolder> {

    private Context context;
    private List<SerialListResponse.ResponseBean> list;
    private AdapterListener listener;

    public void setAdapterListener(AdapterListener listener) {
        this.listener = listener;
    }

    public SerialListAdapter(Context context, List<SerialListResponse.ResponseBean> list) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_serial_list_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
        //封面
        GlideUtils.loadImageView(context, viewHolder.bookCover, list.get(i).getImgUrl(),
                R.drawable.default_book_cover, R.drawable.default_book_cover);
        //书名
        viewHolder.bookName.setText(list.get(i).getTitle());
        //作者
        viewHolder.bookAuthor.setText(list.get(i).getAuthor());
        //章节
        int chapterNumber = list.get(i).getChapterNumber();
        String chatperMsg = list.get(i).getState() == 2
                ? "共" + chapterNumber + "章" : "更新至第" + chapterNumber + "章";
        viewHolder.bookItem.setText(chatperMsg);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, SerialDetailsActivity.class);
                intent.putExtra(Constant.INTENT_DATA, list.get(i));
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView bookName, bookAuthor, bookItem;
        private ImageView bookCover;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            bookCover = itemView.findViewById(R.id.book_cover);
            bookName = itemView.findViewById(R.id.book_name);
            bookAuthor = itemView.findViewById(R.id.book_author);
            bookItem = itemView.findViewById(R.id.book_item);
        }
    }
}
